<!--
 * @Description: 客户类型设置列表
 * @Author: liyujie
 * @Date: 2021/07/08
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/07/08
-->
<template>
    <section class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    客户类型设置
                </div>
            </div>
            <!-- /header -->
            <div
                class="btn-view"
                @click="showDialog('add')"
            >
                <i class="el-icon-plus"/>
                创建客户类型
            </div>
            <!--筛选-->
            <!-- table -->
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                    ref="table"
                    class="data-table"
                    :cell-style="{height: '62px'}"
                    v-loading="tableLoading"
                >
                    <el-table-column
                        label="序号"
                        align="center"
                        width="80"
                        type="index"
                        fixed="left"
                    />
                    <el-table-column
                        prop="title"
                        label="客户类型"
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        label="操作"
                        align="left"
                        width="420"
                    >
                        <template slot-scope="scope">
                            <div class="option-btn-group">
                                <el-button
                                    type="text"
                                    size="medium"
                                    @click="showSettingDialog(scope.row, 'edit')"
                                >
                                    设置店铺预算方案
                                </el-button>
                                <el-button
                                    type="text"
                                    size="medium"
                                    @click="showSettingDialog(scope.row, 'detail')"
                                >
                                    查看店铺预算方案
                                </el-button>
                                <el-button
                                    type="text"
                                    size="medium"
                                    @click="showDialog('edit', scope.row)"
                                >
                                    编辑
                                </el-button>
                                <el-button
                                    type="text"
                                    size="medium"
                                    @click="handleRoute('ShopList', {shop_group: scope.row.shop_group})"
                                >
                                    查看店铺信息
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- /table -->
            <!-- /分页 -->
            <!--<div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>-->
        </div>
        <el-dialog
            :title="dialog.type === 'add' ? '创建客户类型' : '编辑客户类型'"
            :visible.sync="dialog.dialogVisible"
            width="640px"
            :close-on-click-modal="false"
            @close="handleClose"
        >
            <div class="dialog-content">
                <div class="title">客户类型名称</div>
                <el-input
                    v-model="dialog.title"
                    size="medium"
                    placeholder="请输入客户类型名称"
                    style="width: 512px"
                    @keydown.enter.native="setCustomerName()"
                />
            </div>
            <div
                class="dialog-footer"
                style="padding-top: 12px"
            >
                <el-button
                    size="small"
                    style="width: 100px"
                    @click="dialog.dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="small"
                    style="width: 100px;margin-left: 52px"
                    :disabled="!dialog.title"
                    @click="setCustomerName()"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>
        <!-- 设置补贴弹窗 -->
        <budget-setting-dialog
            ref="budgetSettingDialog"
            @confirm="handleSetMoney(shop_group, $event)"
        />
    </section>
</template>

<script>
import { back } from '@/mixin/back';
import BudgetSettingDialog from "@/components/financial-control/budget-setting-dialog";

export default {
    name: 'CustomerTypeList',
    mixins: [back],
    components: {BudgetSettingDialog},
    data() {
        return {
            filterData: {},
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dialog: {
                type: 'add',
                dialogVisible: false,
                title: '',
            },
            shop_group: '',
            authResult: {}
        };
    },
    computed: {},
    filters: {},
    watch: {},
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '财控管理'}, {title: '店铺补贴风控', path: '/student/shop-control'}, {title: '客户类型设置'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            this.$post('/student/shop_group%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    // this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 自定义表格标题hover提示
         */
        renderHeader(h,title, content){
            return h('span', {}, [
                h('span', {style: 'font-weight: bold'}, title),
                h('el-popover', { props: { placement: 'top-start', width: '200', trigger: 'hover', content: content }}, [
                    h('i', { slot: 'reference', class:'el-icon-question', style: 'color:#666;margin-left: 6px'}, '')
                ])
            ]);
        },

        /**
         * 设置客户类型名称
         */
        setCustomerName() {
            let params = {
                shop_group: this.shop_group,
                title: this.dialog.title,
            };
            let url = this.shop_group ? '/student/shop_group%5Cset' : '/student/shop_group%5Cadd';

            this.$post(url, params, res => {
                if (res.code === 1) {
                    this.dialog.dialogVisible = false;
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '保存成功',
                        type: 'success'
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 显示编辑客户类型名称弹窗
         * @param type
         * @param row
         */
        showDialog(type, row) {
            this.dialog.type = type;
            if(row) {
                this.dialog.title = row.title;
                this.shop_group = row.shop_group;
            } else {
                this.shop_group = '';
            }
            this.dialog.dialogVisible = true;
        },

        /**
         * 弹窗关闭
         */
        handleClose() {
            this.shop_group = '';
            this.dialog.title = '';
        },

        /**
         * 显示设置弹窗
         * @param row
         * @param type
         */
        showSettingDialog(row, type) {
            if(!this.authResult.auth_set) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.tableLoading = true;
            this.$post('/student/shop_group%5Cget_shop_control_detail', {shop_group: row.shop_group}, res => {
                if (res.code === 1) {
                    // TODO:是否要处理数据
                    this.shop_group = row.shop_group;
                    this.$refs.budgetSettingDialog.show(type, res.data);
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 确认设置补贴预算
         * @param shop_group
         * @param data
         */
        handleSetMoney(shop_group, data) {
            let params = {
                shop_group,
                ...data
            };

            this.$post('/student/shop_group%5Csave_shop_control', params, res => {
                if (res.code === 1) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success'
                    });
                    this.$refs.budgetSettingDialog.close();
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
            });
        }
    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }
        .btn-view {
            margin: 24px 32px;
            height: 36px;
            background: rgba(32, 160, 255, 0.1);
            border-radius: 4px;
            border: 1px solid #20A0FF;
            font-size: 14px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #20A0FF;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            .el-icon-plus {
                margin-right: 10px;
            }
        }

        // 表格
        .table-wrapper {
            padding: 0 24px;
            flex: 1;
            height: 0;
            .cell-title {
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #333333;
                margin-bottom: 12px;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                -webkit-line-clamp: 2;
            }
            .cell-content {
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                -webkit-line-clamp: 3;
            }
            .cell-tips {
                color: #999;
            }
        }
        .data-table {
            ::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }
            // 操作按钮
            .option-btn-group {
                .el-button {
                    margin-left: 0;
                    margin-right: 10px;
                }
            }
        }
        .status-wrapper {
            display: flex;
            align-items: center;
            justify-content: center;

            .status-dot {
                width: 6px;
                height: 6px;
                border-radius: 50%;
                margin-right: 5px;

                &.success {
                    background: #4CD964;
                }

                &.danger {
                    background: #FF3030;
                }

                &.warning {
                    background: #FFA72D;
                }

                &.normal {
                    background: #A6A6A6;
                }
            }
        }

        /* 分页 */
        .pagination-wrapper {
            padding: 12px 24px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    .dialog-content {
        display: flex;
        flex-direction: column;
        padding: 0 32px;
        .title {
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: rgba(0, 0, 0, 0.85);
            margin-bottom: 12px;
        }
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
        margin-top: 24px;
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-dialog__body {
        padding: 24px 32px;
    }
    /deep/ .el-textarea__inner {
        resize: none;
    }
    /deep/ .el-table__fixed-right {
        right: 6px!important;
    }
}
</style>
